package com.andy.ui.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.andy.ui.R;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

/**
 * @Author: luofan
 * @CreateDate: 2020/3/31 15:43
 * @Description:
 */
public class SimpleFragmentActivity extends BaseActivity {

    private static final String FRAGMENT_NAME = "fragmentName";

    @Override
    public int getLayoutId() {
        return R.layout.activity_simple_fragment;
    }

    @Override
    public void initLogic() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        String fragmentName = getIntent().getStringExtra(FRAGMENT_NAME);
        Fragment fragment = fragmentManager.getFragmentFactory().instantiate(getClassLoader(), fragmentName);
        fragment.setArguments(getIntent().getExtras());
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.layout_frame, fragment);
        fragmentTransaction.commitAllowingStateLoss();
    }

    public static void startMe(Context context, String fragmentName, Bundle bundle) {
        Intent intent = new Intent(context, SimpleFragmentActivity.class);
        intent.putExtra(FRAGMENT_NAME, fragmentName);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }
}
